package rsee.com;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ExAdapter extends BaseExpandableListAdapter {
	public static final String G_TEXT = "g_text";
	public static final String C_TEXT1 = "c_text1";

	private Context mContext = null;
	private int mShowType = 0;
	private String mRecordNames[] = null;
	private int mRecordChanNums[] = null;
	private List<Map<String, String>> mGroupData = null;
	private List<List<Map<String, String>>> mChildData = null;
	
	public ExAdapter(Context context) {
		super();
		mContext = context;
		
		mGroupData = new ArrayList<Map<String, String>>();
		mChildData = new ArrayList<List<Map<String, String>>>();
	}
	
	public void clear() {
		if (null != mGroupData)
			mGroupData.clear();
		
		if (null != mChildData)
			mChildData.clear();
	}
	
	public void setShowType(int showType) {
		mShowType = showType;
	}
	
	public void setRecordNames(String[] names) {
		mRecordNames = names;
	}
	
	public void setRecordChanNums(int[] channs) {
		mRecordChanNums = channs;
	}
	
	public void addGroupData(Map<String, String> group) {
		mGroupData.add(group);
	}
	
	public void addChildData(List<Map<String, String>> child) {
		mChildData.add(child);
	}
	
	//================ BaseExpandableListAdapter ================
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.member_listview, null);
		}
		final TextView title = (TextView) view
				.findViewById(R.id.content_001);
		title.setText(getGroup(groupPosition).toString());
		ImageView button = (ImageView) view
				.findViewById(R.id.fuckallButton);
		button.setBackgroundResource(R.drawable.info);
		
//		if (mShowType != RecordDialog.ShowType.TypeLoad)
//			button.setVisibility(View.GONE);
		button.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			 RecordDialog.allPlay(groupPosition);
			}			
		});
		
		ImageView image = (ImageView) view.findViewById(R.id.image);

		image.setBackgroundResource(R.drawable.chat_viode);

		final TextView title2 = (TextView) view
				.findViewById(R.id.channelText);
		
		if (mRecordChanNums[groupPosition] > 0) {
			String channel = Integer.toString(mRecordChanNums[groupPosition]);
			title2.setText(channel);
		} else
			title2.setText("?");
		
		return view;
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public Object getGroup(int groupPosition) {
		return mGroupData.get(groupPosition).get(G_TEXT).toString();
	}

	public int getGroupCount() {
		return mGroupData.size();

	}

	// **************************************
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.member_childitem, null);
		}
		final TextView title = (TextView) view
				.findViewById(R.id.child_text);
		title.setText(mChildData.get(groupPosition).get(childPosition)
				.get(C_TEXT1).toString());
		ImageView iamge = (ImageView) view.findViewById(R.id.imageCh);
		iamge.setBackgroundResource(R.drawable.chat_viode);
		return view;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public Object getChild(int groupPosition, int childPosition) {
		return mChildData.get(groupPosition).get(childPosition).get(C_TEXT1)
				.toString();
	}

	public int getChildrenCount(int groupPosition) {
		return mChildData.get(groupPosition).size();
	}

	// **************************************
	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}